package com.example.cartserver.service;


import com.example.cartserver.dao.CartDao;
import com.example.cartserver.entities.Cart;
import com.example.server_engineer.controller.utils.R;
import com.example.server_engineer.service.AbstractTypedService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


/**
 * @author Administrator
 */
@Slf4j
@Service
public class CartService
        extends AbstractTypedService<Cart, Integer> {

    CartDao cartDao;

    CartService(CartDao dao) {
        this.dataContext = dao;
        this.cartDao = dao;
    }

    public List<Cart> queryAll() {
        List<Cart> lst = null;
        try {
            lst = ((CartDao) this.dataContext).findAll();
            log.info("query carts: " + lst.size());
        } catch (Exception e) {
            log.warn(e.toString());
        }
        return lst;
    }

    // 查看用户购物车信息
    public List<Cart> queryCartByUserId(String userId) {
        List<Cart> cartList = null;
        try {
            cartList = cartDao.findCartByUserId(userId);
            log.info("userid:" + userId + " cartList:" + cartList.size());
        } catch (Exception e) {
            log.warn(e.toString());
        }
        return cartList;
    }

    // 根据userid businessid查看购物车的信息
    public List<Cart> queryByBusinessIdAndUserId(Integer businessId,String userid){
        List<Cart> cartList = null;
        try {
            cartList = cartDao.findCartByBusinessIdAndUserId(businessId,userid);
            log.info("cartList:" + cartList.size());
//            cartList.get(0).setLast_modified_date(new Date());
        } catch (Exception e) {
            log.warn(e.toString());
        }
        return cartList;
    }

    @Resource
    FoodFeignService foodFeignService;

    // 根据商家的id和用户id计算订单总价
//    public Double computeTotalPrice(Integer businessId,String userId){
//        // 先根据商家和用户id查出所有的 cartList
//        List<Cart> cartList = queryByBusinessIdAndUserId(businessId,userId);
//        double total_price = 0;
//        // 找出每个foodid所对应的价格 price
//        for (Cart item : cartList){
//            Integer foodid = item.getFoodId();
////            Food food = foodDao.findFoodById(foodid);
//            R result = foodFeignService.getFood(foodid);
//// 根据 foodid 查询 food 价格
//            total_price += item.getQuantity() * result.getData().getPrice();
//        }
//
//        return total_price;
//    }



    // 查看用户购物车的食物信息
    public R queryCartInfo(Integer cartid){
        Optional<Cart> carts = cartDao.findById(cartid);
        Integer foodid = carts.get().getFoodId();
//        Food food = foodDao.findFoodById(foodid);
        R result =foodFeignService.getFood(foodid);

//        String res = carts.get() +"\n" + food.toString();
        return result;

    }

    // 根据用户id和商家id和食物id查看购物车中食物的数量
    public Cart queryFoodnum(String userid,int businessid,int foodid){
        return cartDao.findCartByUserIdAndBusinessIdAndFoodId(String.valueOf(userid),businessid,foodid);
    }

    public Page<Cart> queryCartByPage(Pageable pageable) {
        return cartDao.queryPage(pageable, null);
    }
}
